﻿using System;

using System.Diagnostics;
using System.Collections.Generic;
using Windows.Storage;

namespace RobertMcLaws.TwilioForWin8
{
    public class AppSettings
    {

        #region Constants

        // The isolated storage key names of our settings
        const string AccountSidSettingKeyName = "AccountSidSetting";
        const string ApiKeySettingKeyName = "ApiKeySetting";

        // The default value of our settings
        const string AccountSidSettingDefault = "";
        const string ApiKeySettingDefault = "";

        #endregion

        #region Private Members

        // Our isolated storage settings
        static readonly ApplicationDataContainer IsolatedStore;

        #endregion

        #region Properties

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public static string AccountSidSetting
        {
            get
            {
                return GetValueOrDefault(AccountSidSettingKeyName, AccountSidSettingDefault);
            }
            set
            {
                AddOrUpdateValue(AccountSidSettingKeyName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Password Setting Key.
        /// </summary>
        public static string ApiKeySetting
        {
            get
            {
                return GetValueOrDefault(ApiKeySettingKeyName, ApiKeySettingDefault);
            }
            set
            {
                AddOrUpdateValue(ApiKeySettingKeyName, value);
                Save();
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        static AppSettings()
        {
            try
            {
                // Get the settings for this application.
                IsolatedStore = Windows.Storage.ApplicationData.Current.LocalSettings;

            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            // If the value has changed
            if (IsolatedStore.Values[Key] != value)
            {
                // Store the new value
                IsolatedStore.Values[Key] = value;
                valueChanged = true;
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T GetValueOrDefault<T>(string key, T defaultValue)
        {
            T value;
            if (IsolatedStore == null) return defaultValue;

            // If the key exists, retrieve the value.
            if (IsolatedStore.Values[key] != null)
            {
                value = (T)IsolatedStore.Values[key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public static void Save()
        {
            //IsolatedStore.Save();
        }

        #endregion

    }
}
